package com.shengsheng.expschedule.service.expschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shengsheng.expschedule.service.expschedule.entity.Schedule;
import com.shengsheng.expschedule.service.expschedule.entity.Vo.ScheduleQueryVo;
import com.shengsheng.expschedule.service.expschedule.entity.Vo.WeekHeaderVo;
import com.shengsheng.expschedule.service.expschedule.mapper.ScheduleMapper;
import com.shengsheng.expschedule.service.expschedule.service.ScheduleService;
import com.shengsheng.expschedule.service.expschedule.service.SemesterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 实验室选课表 服务实现类
 * </p>
 *
 * @author yuan
 * @since 2021-09-27
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {

    @Autowired
    private SemesterService semesterService;


    @Override
    public Map<String, List> getScheduleByScheduleQueryVo(ScheduleQueryVo scheduleQueryVo) {

        QueryWrapper<Schedule> scheduleQueryWrapper = new QueryWrapper<>();
        scheduleQueryWrapper.orderByAsc("week")
                .orderByAsc("time")
                .eq("week_num", scheduleQueryVo.getWeekNum())
                .eq("semester_id", scheduleQueryVo.getSemesterId());
        List<Schedule> realScheduleList = baseMapper.selectList(scheduleQueryWrapper);

        String semesterId = scheduleQueryVo.getSemesterId();
        Integer weekNum = scheduleQueryVo.getWeekNum();
        ArrayList<Schedule> allScheduleList = new ArrayList<>();
        for (int i = 1; i <= 7; i++) {
            for (int j = 1; j <= 5; j++) {
                allScheduleList.add(getSchedule(semesterId, weekNum, i, j, realScheduleList));
            }
        }

        return changeScheduleData(semesterId, weekNum, allScheduleList);
    }

    @Override
    public boolean isExistSemesterId(String semesterId) {

        QueryWrapper<Schedule> scheduleQueryWrapper = new QueryWrapper<>();
        scheduleQueryWrapper.eq("semester_id", semesterId);
        return baseMapper.selectList(scheduleQueryWrapper).size() > 0;
    }

    @Override
    public void updateDate(Long betweenDays) {

        // 更新所有的日期
        baseMapper.updateDate(betweenDays);
    }

    /**
     * 组装所有的课程数据，未选课的用空对象代替
     *
     * @param week
     * @param time
     * @param scheduleList
     * @return
     */
    private Schedule getSchedule(String scheduleId, Integer weekNum, Integer week, Integer time, List<Schedule> scheduleList) {
        for (Schedule schedule : scheduleList) {
            if (schedule.getWeek() == week && schedule.getTime() == time) return schedule;
        }
        Schedule schedule = new Schedule();
        schedule.setSemesterId(scheduleId);
        schedule.setWeekNum(weekNum);
        schedule.setWeek(week);
        schedule.setTime(time);
        return schedule;
    }

    /**
     * 将课程表按行的格式传递给前端
     *
     * @param allScheduleList
     * @return
     */
    private Map<String, List> changeScheduleData(String scheduleId, Integer weekNum, List<Schedule> allScheduleList) {

        List<WeekHeaderVo> weekHeaderRow = new ArrayList<>();
        WeekHeaderVo weekHeaderVoBasic = new WeekHeaderVo();
        weekHeaderVoBasic.setWeekDescription("节数/每节");
        weekHeaderRow.add(weekHeaderVoBasic);
        // 本学期起始日期
        Date originDate = semesterService.getOriginDate(scheduleId);
        for (int i = 1; i <= 7; i++) {
            WeekHeaderVo weekHeaderVo = new WeekHeaderVo();
            weekHeaderVo.setWeekDescription(getWeekDescription(i));
            Date date = getDateAfter(originDate, (weekNum - 1) * 7 + i - 1);
            weekHeaderVo.setWeekDate(date);
            weekHeaderRow.add(weekHeaderVo);
        }

        List<Schedule> Time1Row = new ArrayList<>();
        List<Schedule> Time2Row = new ArrayList<>();
        List<Schedule> Time3Row = new ArrayList<>();
        List<Schedule> Time4Row = new ArrayList<>();
        List<Schedule> Time5Row = new ArrayList<>();

        for (Schedule schedule : allScheduleList) {
            if (schedule.getTime() == 1) Time1Row.add(schedule);
            else if (schedule.getTime() == 2) Time2Row.add(schedule);
            else if (schedule.getTime() == 3) Time3Row.add(schedule);
            else if (schedule.getTime() == 4) Time4Row.add(schedule);
            else if (schedule.getTime() == 5) Time5Row.add(schedule);
        }
        List<List<Schedule>> lists = new ArrayList<>();
        lists.add(Time1Row);
        lists.add(Time2Row);
        lists.add(Time3Row);
        lists.add(Time4Row);
        lists.add(Time5Row);
        HashMap<String, List> map = new HashMap<>();
        map.put("weekHeaderRow", weekHeaderRow);
        map.put("rowList", lists);
        return map;
    }

    /**
     * 获取几天后的日期
     *
     * @param date
     * @param day
     * @return
     */
    private Date getDateAfter(Date date, int day) {
        Calendar now = Calendar.getInstance();

        now.setTime(date);

        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);

        return now.getTime();

    }

    /**
     * 根据第几天判断是周几
     *
     * @param day
     * @return
     */
    private String getWeekDescription(int day) {
        if (day == 1) return "周一";
        else if (day == 2) return "周二";
        else if (day == 3) return "周三";
        else if (day == 4) return "周四";
        else if (day == 5) return "周五";
        else if (day == 6) return "周六";
        else if (day == 7) return "周日";
        return null;
    }
}
